package org.app.Service.Impl;

import jakarta.annotation.Resource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.subject.Subject;
import org.app.Config.JWTToken;
import org.app.Entity.User;
import org.app.Entity.comm.Result;
import org.app.Mapper.IUserMapper;
import org.app.Service.IUserService;
import org.app.Utils.CaptchaUtil;
import org.app.Utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.awt.image.BufferedImage;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class UserService implements IUserService {
    @Resource
    private IUserMapper userMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public Result<User> Login(String c_phone, String c_password){
        Result<User> result = new Result<>();
        if (userMapper.Login(c_phone,c_password) != null){
            User user = userMapper.Login(c_phone,c_password);
            result.setCode(200);
            result.setMessage("通过");

            String token = JwtUtil.getUserToken(user);
            user.setToken(token);
            userMapper.saveToken(user);
            result.setData(user);
        }else {
            result.setData(null);
            result.setCode(500);
            result.setMessage("手机号或密码错误");
        }
        return result;
    }
    @Override
    public Result<Integer> Register(User user) {
        Result<Integer> result = new Result<>();
        String phone = user.getC_phone();

        // 检查用户是否已存在
        if (userMapper.selectUserByPhone(phone) != null) {
            result.setCode(500);
            result.setMessage("用户已存在");
            result.setData(0);
        } else {
            // 如果头像为空，设置默认头像路径
            if (user.getC_img() == null || user.getC_img().isEmpty()) {
                user.setC_img("@/static/cat.png");
            }
            user.setToken("null");
            // 执行注册逻辑
            result.setData(userMapper.Register(user));
            if (result.getData() == 1) {
                result.setCode(200);
                result.setMessage("注册成功");
            } else {
                result.setCode(500);
                result.setMessage("注册失败");
            }
        }
        return result;
    }

    @Override
    public Result<Integer> updateUserInfo(User user){
        Result<Integer> result = new Result<>();
        String c_id = user.getC_id();
        if(userMapper.selectById(c_id)!=null){
            result.setData(userMapper.updateUserInfo(user));
            if(result.getData() != null){
                result.setCode(200);
                result.setMessage("通过");
            }else {
                result.setCode(500);
                result.setMessage("有错误");
            }
        } else {
            result.setCode(500);
            result.setMessage("未能找到用户");
            result.setData(0);
        }
        return result;
    }


    @Override
    public Result<Integer> updateUserPassword(String username, String phone, String new_password) {
        Result<Integer> result = new Result<>();
        // 查询用户信息
        User user = userMapper.selectByPhoneAndUsername(phone, username);

        if (user != null) {
            // 如果找到用户，则更新密码
            user.setC_password(new_password);
            int updateResult = userMapper.updateUserPassword(username, phone,new_password);
            if (updateResult > 0) {
                result.setCode(200);
                result.setMessage("密码更新成功");
                result.setData(updateResult);
            } else {
                result.setCode(500);
                result.setMessage("密码更新失败");
                result.setData(0);
            }
        } else {
            // 用户不存在
            result.setCode(500);
            result.setMessage("未找到用户");
            result.setData(0);
        }
        return result;
    }


    public Result<User> selectUserById(String c_id){
        Result<User> result = new Result<>();
        result.setCode(200);
        User user = userMapper.getById(c_id);
        result.setData(user);
        result.setMessage("已找到");
        return result;
    }


    public Set<String> getUserRoles(String c_id){
        return userMapper.getUserRoles(c_id);
    }
}


